<?php
global $_W, $_GPC;
$_GPC['do'] = 'quickmenu';
$_GPC['menu'] = 'card';

if ($_GPC['act'] == 'edit') {
    $id = intval($_GPC['id']);
    if ($_W['ispost']) {
        $data = array();
        $data['uniacid'] = $_W['uniacid'];
        $data['link'] = trim($_GPC['link']);
        $data['title'] = trim($_GPC['title']);
        $data['name'] = trim($_GPC['name']);
        $data['ido'] = trim($_GPC['ido']);
        $data['icon'] = trim($_GPC['icon']);
        $data['sort'] = intval($_GPC['sort']);
        $data['status'] = trim($_GPC['status']);
        $data['create_time'] = time();

        // 名称和链接不能为空
        if (!$data['title'] || !$data['link']) {
            message('名称和链接不能为空', referer(), 'error');
        }

        if (!$data['icon']) {
            message('请上传图标', referer(), 'error');
        }

        if (!empty($id)) {
            $data['id'] = $id;
            unset($data['create_time']);
        }
        M('quickmenu')->update($data);
        message('保存成功', $this->createWebUrl('quickmenu'), 'success');
    }
    $item = M('quickmenu')->getInfo($id);
    include $this->template('quickmenu_edit');
    exit;
}

if ($_GPC['act'] == 'reset') {
    $resetData = array(
        array('首页', '首页', 'weidun_card/pages/index/index', 'icon iconfont icon-homefill', 'index', 3),
        array('热点', '热点', 'weidun_card/pages/news/news', 'icon iconfont icon-news_hot_fill_light', 'news', 2),
        array('我的', '我的', 'weidun_card/pages/my/my', 'icon iconfont icon-peoplefill', 'my', 1),
    );
    M('quickmenu')->deleteByParams();
    foreach ($resetData as $key => $value) {
        $item = array(
            'title' => $value[0],
            'name' => $value[1],
            'link' => $value[2],
            'icon' => $value[3],
            'ido' => $value[4],
            'sort' => $value[5],
            'status' => 1,
            'create_time' => time()
        );
        M('quickmenu')->update($item);
    }
    message('重置成功', $this->createWebUrl('quickmenu'), 'success');
}

if ($_GPC['act'] == 'delete') {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        message('参数错误', referer(), 'error');
    }
    M('quickmenu')->delete($id);
    message('删除成功', referer(), 'success');
}

$page = !empty($_GPC['page']) ? intval($_GPC['page']) : 1;
$where = "";
$list = M('quickmenu')->getList($page, $where, array(), 10, 'sort desc');
foreach ($list['list'] as $key => $value) {
    $list['list'][$key]['status'] = $value['status'] ? '启用' : '隐藏';
}
$iconColor = $this->pro['icon_color'];
$iconHoverColor = $this->pro['icon_hover_color'];

include $this->template('quickmenu');